package guiBeatporty;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

import java.awt.Color;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import Controle.Controlador;
import Controle.TabelaDeMusicasModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Vector;

public class CarrinhoDeCompras extends JInternalFrame {
	private JTable tableCarrinho;
	private static DefaultTableModel modelCarrinho; 
	private static JTextField txtPrecoTotal;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CarrinhoDeCompras frame = new CarrinhoDeCompras();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CarrinhoDeCompras() {
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setBounds(100, 100, 531, 380);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JButton btnFecharCompra = new JButton("Fechar Compra");
		btnFecharCompra.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for(int i = 0; i < tableCarrinho.getRowCount(); i++){
					modelCarrinho.removeRow(i);
				}
				Controlador.getControlador().getPedidoCorrente().setValorTotal(0.0);
				Controlador.getControlador().getCarrinho().clear();
				Controlador.getControlador().getPedidoCorrente().getVetorItens().clear();			
				txtPrecoTotal.setText(Controlador.getControlador().converteDoubleString(Controlador.getControlador().getPedidoCorrente().getValorTotal()));
				try {
					Controlador.getControlador().salvarDados();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				MainFrame.atualizarInfoPedido();
				MainFrame.resizeMainFrame(true);
				dispose();
			}
		});
		
		JButton btnExcluir = new JButton("Excluir");
		btnExcluir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Controlador.getControlador().getCarrinho().remove(tableCarrinho.getSelectedRow());
				Controlador.getControlador().getPedidoCorrente().getVetorItens().remove(tableCarrinho.getSelectedRow());
				Controlador.getControlador().getPedidoCorrente().decrementaValorTotal();
				modelCarrinho.removeRow(tableCarrinho.getSelectedRow());
				try {
					Controlador.getControlador().salvarDados();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				MainFrame.atualizarInfoPedido();
				txtPrecoTotal.setText(Controlador.getControlador().converteDoubleString(Controlador.getControlador().getPedidoCorrente().getValorTotal()));
				
				
				/*if(modelCarrinho.getDataVector().size() != -1){
					Vector linhas = new Vector();
					linhas = (Vector) model.getDataVector();
					Vector strings = new Vector();
					strings = (Vector) linhas.get(tableCarrinho.getSelectedRow());
							 
					String nome = (String) strings.get(0);
					String artista = (String) strings.get(1);
					String gravadora = (String) strings.get(2);
					String lancamento = (String) strings.get(3);
					String estilo = (String) strings.get(4);
				}*/
			
			}
		});
		
		JLabel lblTotal = new JLabel("Total:");
		
		txtPrecoTotal = new JTextField();
		txtPrecoTotal.setColumns(10);
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MainFrame.resizeMainFrame(true);
				dispose();
			}
		});
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblTotal)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(txtPrecoTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(96)
					.addComponent(btnExcluir)
					.addContainerGap(242, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(200)
					.addComponent(btnFecharCompra)
					.addContainerGap(224, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(229)
					.addComponent(btnOk)
					.addContainerGap(253, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 227, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(34)
							.addComponent(btnFecharCompra)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnOk))
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblTotal)
							.addComponent(txtPrecoTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(btnExcluir)))
					.addContainerGap(24, Short.MAX_VALUE))
		);
		
		modelCarrinho = new DefaultTableModel();
		tableCarrinho = new JTable();
		tableCarrinho.setModel( modelCarrinho = new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Nome", "Artista", "Gravadora", "Estilo", "Lan\u00E7amento"
			}
		));
		
		txtPrecoTotal.setText(Controlador.getControlador().converteDoubleString(Controlador.getControlador().getPedidoCorrente().getValorTotal()));
		
		scrollPane.setViewportView(tableCarrinho);
		getContentPane().setLayout(groupLayout);
		this.setTabela();

	}
	
	private void setTabela(){
		for(int i = 0; i < Controlador.getControlador().getPedidoCorrente().getVetorItens().size(); i++){
			
			
			Object[] data = new Object[] {
					Controlador.getControlador().getCarrinho().get(i).getNome(), Controlador.getControlador().getCarrinho().get(i).getArtista(),Controlador.getControlador().getCarrinho().get(i).getGravadora(),
					Controlador.getControlador().getCarrinho().get(i).getEstilo(),Controlador.getControlador().converteDataString(Controlador.getControlador().getCarrinho().get(i).getDataLanc())
				};
			modelCarrinho.addRow(data);
		}
		modelCarrinho.fireTableDataChanged();
	}
}
